from PyQt5.QtGui import QIcon, QFont, QPalette, QColor, QBrush, QPixmap
from PyQt5.QtWidgets import *
from PyQt5.QtCore import QCoreApplication, QRect, Qt
import sys
import save_to_csv as USE_FUNC
from PyQt5 import QtWidgets

# pyinstaller -F -w -i .\icon.ico -n GetBfans  show.py
class MukuchiChatDemo(QWidget):
    print('')

    def __init__(self, parent=None):
        super(MukuchiChatDemo, self).__init__(parent)
        self.set_palette()
        # self.setWindowFlags(Qt.FramelessWindowHint)#取消边框
        self.initUI()

    def initUI(self):
        # 提示字体设置
        QToolTip.setFont(QFont('微软雅黑', 10))
        self.setToolTip('❤❤❤')
        font = QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(10)
        lab_accept = QLabel('<h4><font color=black>用户信息:</font></h4>', self)
        lab_display = QLabel('Tips：', self)
        lab_display1 = QLabel('1、导出-0不获取粉丝的粉丝数，只导出基本信息', self)
        lab_display2 = QLabel('2、导出-1获取粉丝的粉丝数，但速度较慢', self)
        lab_display3 = QLabel('3、b站限制只能查看1000个粉丝', self)
        lab_display4 = QLabel('4、SESSIONDATA从浏览器cookie中获取', self)
        # 创建文本框
        self.text_UID = QLineEdit("", self)
        self.text_UID.setToolTip('在这里键入<b>要获取的用户UID</b>')
        self.text_UID.setPlaceholderText("UID")

        self.text_SESSIONDATA = QLineEdit("",self)  # QTextBrowser(self)
        self.text_SESSIONDATA.setPlaceholderText("SESSIONDATA")
        self.text_SESSIONDATA.setToolTip('输入登录后的<b>SESSIONDATA</b>')


        # 创建3个按钮
        setbtn = QPushButton(self.setFont(font))
        
        self.btnPress0 = QPushButton('导出-0', self)
        self.btnPress0.setToolTip('不获取每个粉丝的粉丝数')
        self.btnPress1 = QPushButton('导出-1', self)
        self.btnPress1.setToolTip('获取每个粉丝的粉丝数(用时较长)')
        self.btnPress2 = QPushButton('清 空', self)
        # 设置位置
        lab_accept.setGeometry(30, 20, 300, 30)  # 用户信息
        self.text_UID.setGeometry(30, 65, 400,40)  # 输入框-1
        self.text_SESSIONDATA.setGeometry(30, 125, 400, 40)  # 输入框-2
        self.btnPress0.setGeometry(30, 185, 120, 35)  # 导出-0
        self.btnPress1.setGeometry(170, 185, 120, 35)  # 导出-1
        self.btnPress2.setGeometry(310, 185, 120, 35)  # 清空
        lab_display.setGeometry(30, 235, 400, 30)
        lab_display1.setGeometry(30, 265, 400, 30)
        lab_display2.setGeometry(30, 295, 400, 30)
        lab_display3.setGeometry(30, 325, 400, 30)
        lab_display4.setGeometry(30, 355, 400, 30)
        # 将按钮的点击信号与相关的槽函数进行绑定，点击即触发
        self.btnPress0.clicked.connect(self.btnPress0_clicked)
        self.btnPress1.clicked.connect(self.btnPress1_clicked)
        self.btnPress2.clicked.connect(self.btnPress2_clicked)  # 清空按钮
        # 窗口设置
        self.setWindowIcon(QIcon('.\icon.ico'))
        self.setWindowTitle('b站粉丝导出（Made By 茵夏）')
        self.setFixedSize(460, 430)
        self.center()

    def btnPress0_clicked(self):
        SESSIONDATA = self.text_SESSIONDATA.text()
        UID = self.text_UID.text()
        rl=USE_FUNC.get_CSV(SESSIONDATA,UID)
        if rl!=-1:
            QtWidgets.QMessageBox.about(self, "SUCCESS","成功获取到"+str(rl)+"条粉丝数据")
        else:
            QtWidgets.QMessageBox.about(self, "ERROR","获取失败！")

    def btnPress1_clicked(self):
        SESSIONDATA = self.text_SESSIONDATA.text()
        UID = self.text_UID.text()
        rl=USE_FUNC.get_CSV_fan(SESSIONDATA,UID)
        if rl!=-1:
            QtWidgets.QMessageBox.about(self, "SUCCESS","成功获取到"+str(rl)+"条粉丝数据")
        else:
            QtWidgets.QMessageBox.about(self, "ERROR","获取失败！")

    def btnPress2_clicked(self):
        self.text_SESSIONDATA.clear()
        self.text_UID.clear()

    def center(self):
        # ------居中显示方法-------------
        # 获得窗口
        qr = self.frameGeometry()
        # 获得屏幕中心点
        cp = QDesktopWidget().availableGeometry().center()
        # 显示到屏幕中心
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def set_palette(self):
        # ------------设置背景----------------------
        self.setAutoFillBackground(True)
        palette = QPalette()
        palette.setColor(QPalette.WindowText, Qt.black) # 字符颜色
        # palette.setColor(self.backgroundRole(),QColor(192,253,123,100))#设置背景颜色
        # palette.setBrush(self.backgroundRole(), QBrush(QPixmap('bg4.jpg').scaled(400, 600)))
        # self.label.width(), self.label.height()
        self.setPalette(palette)

    # -------退出确认方法--------
    def closeEvent(self, event):

        reply = QMessageBox.question(self, '确认窗口',
                                     "你确定要退出吗?", QMessageBox.Yes |
                                     QMessageBox.No, QMessageBox.No)

        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = MukuchiChatDemo()
    win.show()
    sys.exit(app.exec_())
